package com.zhuliwei.utils;

import com.zhuliwei.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Security工具类
 *
 * @author 朱力维
 * @time 2022/10/14 15:27
 **/
public class SecurityUtils {
    /**
     * 静态方法不需要通过它所属的类的任何实例就可以被调用
     */

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
//        String o = (String) getAuthentication().getPrincipal();
//        System.out.println(getAuthentication().getPrincipal());
        LoginUser loginUser = (LoginUser) getAuthentication().getPrincipal();
        return loginUser;
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isSuperAdmin() {
        String id = getLoginUser().getUser().getId();
        return id != null && "1535f63ffb3748dba254483725ddda1c".equals(id);
    }

    public static String getUserId() {
        return getLoginUser().getUser().getId();
    }

    public static String getNickName() {
        return getLoginUser().getUser().getNickName();
    }

    public static String getIpAddr() {
        return getLoginUser().getUser().getIpaddress();
    }
}